/*
 * @Project Name: spring-boot
 * @File Name: UserService
 * @Package Name: com.hhly.spring.boot.cache
 * @Date: 2017/12/13 17:52
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.service.base.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.base.MaterialMapper;
import com.sm.shoplook.domain.base.Material;
import com.sm.shoplook.service.base.MaterialCategoryService;
import com.sm.shoplook.service.base.MaterialService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.base.material.req.MaterialSearchReqVo;
import com.sm.shoplook.vo.base.material.resp.MaterialRespVo;

/**
 * @author chenpy-1072
 * @desc 基础素材管理
 * @date 2017/12/13 17:52
 * @see
 */
@Service
public class MaterialServiceImpl implements MaterialService {

	private static final Logger LOGGER = LoggerFactory.getLogger(MaterialServiceImpl.class);
	@Autowired
	private MaterialMapper materialMapper;
	@Autowired
	private MaterialCategoryService materialCategoryService;

	@Override
	public ApiResult<PageInfo<MaterialRespVo>> selectMaterialsPage(MaterialSearchReqVo materialSearchReqVo) {
		PageHelper.startPage(materialSearchReqVo.getOffset(), materialSearchReqVo.getPageSize());
		List<MaterialRespVo> materials = materialMapper.selectMaterialsPage(materialSearchReqVo);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, new PageInfo<>(materials));
	}

	@Override
	public ApiResult deleteMaterials(String[] idList) throws ApiException {
		int i = materialMapper.deleteMaterials(idList);
		if (i < 1) {
			throw new ApiException();
		}
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult saveMaterial(Material material) throws ApiException {
		int i;
		if (material.getId() == null) {
			i = materialMapper.insertSelective(material);
		} else {
			i = materialMapper.updateByPrimaryKeySelective(material);
		}
		if (i < 1) {
			throw new ApiException();
		}
		return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult<List<Material>> selectAllMaterials() {
		Material material = new Material();
		material.setStatus(1);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, materialMapper.select(material));
	}

	@Override
	public ApiResult<MaterialRespVo> selectMaterialById(Integer id) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, materialMapper.selectMaterialById(id));
	}

	@Override
	public ApiResult<List<Material>> selectMaterialsByIds(List<Integer> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, materialMapper.selectMaterialsByIds(ids));
	}

	@Override
	public ApiResult<List<Material>> selectMaterialsByCategoryId(Integer categoryId) {
		Material material = new Material();
		material.setStatus(1);
		material.setCategoryId(categoryId);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, materialMapper.select(material));
	}
}
